home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 276_01 / az80.c < prev    next >
C/C++ Source or Header  |  1989-10-01  |  19KB  |  786 lines

  1. /*
  2.     HEADER:        CUG276;
  3.     TITLE:        Z-80 Cross-Assembler (Portable);
  4.     FILENAME:    AZ80.C;
  5.     VERSION:    0.1;
  6.     DATE:        08/27/1988;
  7.     SEE-ALSO:    AZ80.H;
  8.     AUTHORS:    William C. Colley III;
  9. */
  10.  
  11. /*
  12.               Z-80 Cross-Assembler in Portable C
  13.  
  14.         Copyright (c) 1986-1988 William C. Colley, III
  15.  
  16. Revision History:
  17.  
  18. Ver    Date        Description
  19.  
  20. 0.0    JUNE 1988    Derived from my S-6 cross-assembler.  WCC3.
  21.  
  22. 0.1    AUG 1988    Fixed a bug in the command line parser that puts it
  23.             into a VERY long loop if the user types a command line
  24.             like "AZ80 FILE.ASM -L".  WCC3 per Alex Cameron.
  25.  
  26. This file contains the main program and line assembly routines for the
  27. assembler.  The main program parses the command line, feeds the source lines
  28. to the line assembly routine, and sends the results to the listing and object
  29. file output routines.  It also coordinates the activities of everything.  The
  30. line assembly routines uses the expression analyzer and the lexical analyzer
  31. to parse the source line and convert it into the object bytes that it
  32. represents.
  33. */
  34.  
  35. /*  Get global goodies:  */
  36.  
  37. #include "az80.h"
  38.  
  39. /*  Define global mailboxes for all modules:                */
  40.  
  41. char errcode, line[MAXLINE + 1], title[MAXLINE];
  42. int pass = 0;
  43. int eject, filesp, forwd, listhex;
  44. unsigned address, bytes, errors, listleft, obj[MAXLINE], pagelen, pc;
  45. FILE *filestk[FILES], *source;
  46. TOKEN arg, token;
  47.  
  48. /*  Mainline routine.  This routine parses the command line, sets up    */
  49. /*  the assembler at the beginning of each pass, feeds the source text    */
  50. /*  to the line assembler, feeds the result to the listing and hex file    */
  51. /*  drivers, and cleans everything up at the end of the run.        */
  52.  
  53. static int done, ifsp, off;
  54.  
  55. void main(argc,argv)
  56. int argc;
  57. char **argv;
  58. {
  59.     SCRATCH unsigned *o;
  60.     int newline();
  61.     void asm_line();
  62.     void lclose(), lopen(), lputs();
  63.     void hclose(), hopen(), hputc();
  64.     void error(), fatal_error(), warning();
  65.  
  66.     printf("Z-80 Cross-Assembler (Portable) Ver 0.1\n");
  67.     printf("Copyright (c) 1986-1988 William C. Colley, III\n\n");
  68.  
  69.     while (--argc > 0) {
  70.     if (**++argv == '-') {
  71.         switch (toupper(*++*argv)) {
  72.         case 'L':   if (!*++*argv) {
  73.                 if (!--argc) { warning(NOLST);  break; }
  74.                 else ++argv;
  75.                 }
  76.                 lopen(*argv);
  77.                 break;
  78.  
  79.         case 'O':   if (!*++*argv) {
  80.                 if (!--argc) { warning(NOHEX);  break; }
  81.                 else ++argv;
  82.                 }
  83.                 hopen(*argv);
  84.                 break;
  85.  
  86.         default:    warning(BADOPT);
  87.         }
  88.     }
  89.     else if (filestk[0]) warning(TWOASM);
  90.     else if (!(filestk[0] = fopen(*argv,"r"))) fatal_error(ASMOPEN);
  91.     }
  92.     if (!filestk[0]) fatal_error(NOASM);
  93.  
  94.     while (++pass < 3) {
  95.     fseek(source = filestk[0],0L,0);  done = off = FALSE;
  96.     errors = filesp = ifsp = pagelen = pc = 0;  title[0] = '\0';
  97.     while (!done) {
  98.         errcode = ' ';
  99.         if (newline()) {
  100.         error('*');
  101.         strcpy(line,"\tEND\n");
  102.         done = eject = TRUE;  listhex = FALSE;
  103.         bytes = 0;
  104.         }
  105.         else asm_line();
  106.         pc = word(pc + bytes);
  107.         if (pass == 2) {
  108.         lputs();
  109.         for (o = obj; bytes--; hputc(*o++));
  110.         }
  111.     }
  112.     }
  113.  
  114.     fclose(filestk[0]);  lclose();  hclose();
  115.  
  116.     if (errors) printf("%d Error(s)\n",errors);
  117.     else printf("No Errors\n");
  118.  
  119.     exit(errors);
  120. }
  121.  
  122. /*  Line assembly routine.  This routine gets the contents of the    */
  123. /*  argument field from the source file using the expression evaluator    */
  124. /*  and lexical analyzer.  It makes all validity checks on the        */
  125. /*  arguments validity, fills a buffer with the machine code bytes and    */
  126. /*  returns nothing.                            */
  127.  
  128. static char label[MAXLINE];
  129. static int ifstack[IFDEPTH] = { ON };
  130.  
  131. static OPCODE *opcod;
  132.  
  133. void asm_line()
  134. {
  135.     SCRATCH char *p;
  136.     SCRATCH int i;
  137.     int isalph(), popc();
  138.     OPCODE *find_code(), *find_operator();
  139.     void do_label(), flush(), normal_op(), pseudo_op();
  140.     void error(), pops(), pushc(), trash();
  141.  
  142.     address = pc;  bytes = 0;  eject = forwd = listhex = FALSE;
  143.     for (i = 0; i < BIGINST; obj[i++] = NOP);
  144.  
  145.     label[0] = '\0';
  146.     if ((i = popc()) != ' ' && i != '\n') {
  147.     if (isalph(i)) {
  148.         pushc(i);  pops(label);
  149.         for (p = label;  *(p + 1);  ++p);
  150.         if (*p == ':') *p = '\0';
  151.         if (find_operator(label)) { label[0] = '\0';  error('L'); }
  152.     }
  153.     else {
  154.         error('L');
  155.         while ((i = popc()) != ' ' && i != '\n');
  156.     }
  157.     }
  158.  
  159.     trash();  opcod = NULL;
  160.     if ((i = popc()) != '\n') {
  161.     if (!isalph(i)) error('S');
  162.     else {
  163.         pushc(i);  pops(token.sval);
  164.         if (!(opcod = find_code(token.sval))) error('O');
  165.     }
  166.     if (!opcod) { listhex = TRUE;  bytes = BIGINST; }
  167.     }
  168.  
  169.     if (opcod && opcod -> attr & ISIF) { if (label[0]) error('L'); }
  170.     else if (off) { listhex = FALSE;  flush();  return; }
  171.  
  172.     if (!opcod) { do_label();  flush(); }
  173.     else {
  174.     listhex = TRUE;
  175.     if (opcod -> attr & PSEUDO) pseudo_op();
  176.     else normal_op();
  177.     while ((i = popc()) != '\n') if (i != ' ') error('T');
  178.     }
  179.     source = filestk[filesp];
  180.     return;
  181. }
  182.  
  183. static void flush()
  184. {
  185.     while (popc() != '\n');
  186. }
  187.  
  188. static void do_label()
  189. {
  190.     SCRATCH SYMBOL *l;
  191.     SYMBOL *find_symbol(), *new_symbol();
  192.     void error();
  193.  
  194.     if (label[0]) {
  195.     listhex = TRUE;
  196.     if (pass == 1) {
  197.         if (!((l = new_symbol(label)) -> attr)) {
  198.         l -> attr = FORWD + VAL;
  199.         l -> valu = pc;
  200.         }
  201.     }
  202.     else {
  203.         if (l = find_symbol(label)) {
  204.         l -> attr = VAL;
  205.         if (l -> valu != pc) error('M');
  206.         }
  207.         else error('P');
  208.     }
  209.     }
  210. }
  211.  
  212. static void normal_op()
  213. {
  214.     SCRATCH unsigned opcode, tmp;
  215.     unsigned *o, *p;
  216.     static unsigned IM_tbl[] = { 0x00, 0x10, 0x18 };
  217.     int popc();
  218.     unsigned expr(), get_arg();
  219.     void do_label(), error(), grab_comma(), insert_prebyte(), pushc(), trash();
  220.     TOKEN *lex();
  221.  
  222.     do_label();  opcode = opcod -> valu;  o = obj;
  223.     if (opcode > 0xff) *o++ = high(opcode);
  224.     *o = low(opcode);
  225.     switch (opcod -> attr & OPTYPE) {
  226.     case LD:
  227.         p = o;
  228.         switch (get_arg()) {
  229.         case IX:
  230.         case IY:    insert_prebyte(&o,arg.attr);
  231.  
  232.         case BC:
  233.         case DE:
  234.         case HL:
  235.         case SP:    *o = 0x01 + ((arg.attr & 007) << 3);
  236.                 grab_comma();
  237.                 switch (get_arg()) {
  238.                     case NUM_IND:   if (*o == 0x21) *o += 0x09;
  239.                             else {
  240.                             *o += 0x4a;
  241.                             insert_prebyte(&o,
  242.                                 arg.attr);
  243.                             }
  244.  
  245.                     case NUM:        *++o = low(arg.valu);
  246.                             *++o = high(arg.valu);
  247.                             break;
  248.  
  249.                     case IX:
  250.                     case IY:        insert_prebyte(&o,
  251.                             arg.attr);
  252.  
  253.                     case HL:        if (*o == 0x31) {
  254.                             *o = 0xf9;  break;
  255.                             }
  256.  
  257.                     default:        error(arg.attr < NUM ?
  258.                             'R' : 'S');
  259.                             break;
  260.                 }
  261.                 break;
  262.  
  263.         case I:        *o++ = 0xed;  *o = 0x47;  goto do_ld_i;
  264.  
  265.         case R:        *o++ = 0xed;  *o = 0x4f;  goto do_ld_i;
  266.  
  267.         case BC_IND:    *o = 0x02;  goto do_ld_i;
  268.  
  269.         case DE_IND:    *o = 0x12;
  270. do_ld_i:            grab_comma();
  271.                 if (get_arg() != A)
  272.                     error(arg.attr < NUM ? 'R' : 'S');
  273.                 break;
  274.  
  275.         case NUM_IND:    *++o = low(arg.valu);
  276.                 *++o = high(arg.valu);
  277.                 grab_comma();  *p = 0x02;
  278.                 switch (get_arg()) {
  279.                     case A:        *p = 0x32;  break;
  280.  
  281.                     case BC:
  282.                     case DE:
  283.                     case SP:        *p = 0x43;
  284.  
  285.                     case IX:
  286.                     case IY:        *p += (arg.attr & 007)
  287.                             << 3;
  288.                             insert_prebyte(&o,
  289.                             arg.attr);
  290.                             break;
  291.  
  292.                     case HL:        *p = 0x22;  break;
  293.  
  294.                     default:        error(arg.attr < NUM ?
  295.                             'R' : 'S');
  296.                             break;
  297.                 }
  298.                 break;
  299.  
  300.         case A:        grab_comma();
  301.                 switch (get_arg()) {
  302.                     case BC_IND:    *o = 0x0a;  break;
  303.  
  304.                     case DE_IND:    *o = 0x1a;  break;
  305.  
  306.                     case I:        *o++ = 0xed;  *o = 0x57;
  307.                             break;
  308.  
  309.                     case R:        *o++ = 0xed;  *o = 0x5f;
  310.                             break;
  311.  
  312.                     case NUM_IND:   *o++ = 0x3a;
  313.                             *o++ = low(arg.valu);
  314.                             *o = high(arg.valu);
  315.                             break;
  316.  
  317.                     default:        *o += 0x38;  goto do_ld_a;
  318.                 }
  319.                 break;                
  320.  
  321.         case IX_IND:
  322.         case IY_IND:    insert_prebyte(&o,arg.attr);  ++p;
  323.                 *++o = low(arg.valu);  arg.attr = HL_IND;
  324.  
  325.         default:    if (arg.attr <= A) {
  326.                     *p += (arg.attr & 007) << 3;
  327.